package com.youku.adSDK.view.components
{
   import flash.display.Sprite;
   import flash.net.NetConnection;
   import flash.net.NetStream;
   import flash.media.Video;
   import com.youku.adSDK.model.vo.ADData;
   import flash.utils.ByteArray;
   import com.youku.adSDK.YoukuAdManager;
   import com.youku.utils.UrlNetStream;
   import flash.events.NetStatusEvent;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import flash.events.Event;
   import com.youku.adSDK.view.events.ADPlayerEvent;
   import flash.utils.setTimeout;
   import flash.utils.clearTimeout;
   import flash.media.SoundTransform;
   import com.youku.utils.DrawUtil;
   
   public class ADPlayer extends Sprite
   {
      
      public function ADPlayer() {
         super();
         this._playOver = false;
         this._started = false;
         this._preLoad = false;
         this._loadCompleted = false;
      }
      
      protected var _nc:NetConnection;
      
      protected var _ns:NetStream;
      
      protected var _nsObj:Object;
      
      protected var _video:Video;
      
      protected var _layer:Sprite;
      
      protected var _adData:ADData;
      
      protected var _width:Number;
      
      protected var _height:Number;
      
      protected var _vw:Number;
      
      protected var _vh:Number;
      
      protected var _duration:Number;
      
      protected var _started:Boolean;
      
      protected var _playOver:Boolean;
      
      protected var _preLoad:Boolean;
      
      protected var _loadCompleted:Boolean;
      
      protected var _volume:Number = -1;
      
      protected var _timeoutId:Number;
      
      protected var _firstFull:Boolean = false;
      
      protected var _SU:Array;
      
      protected var _id:int = 0;
      
      protected var _isError:Boolean = false;
      
      public function init(param1:ADData, param2:Number, param3:Number) : void {
         var _loc4_:Array = param1.SU;
         var _loc5_:ByteArray = new ByteArray();
         _loc5_.writeObject(_loc4_);
         _loc5_.position = 0;
         this._SU = _loc5_.readObject() as Array;
         this._adData = param1;
         this._width = param2;
         this._height = param3;
         this.createItems();
         this.addEvents();
         this.setWH(this._width,this._height);
      }
      
      protected function createItems() : void {
         this._nc = new NetConnection();
         this._nc.connect(null);
         if(YoukuAdManager.context.HIGHVERSION)
         {
            this._ns = new UrlNetStream(this._nc);
         }
         else if(YoukuAdManager.context.LOWVERSION)
         {
            this._ns = new NetStream(this._nc);
         }
         
         this._ns.bufferTime = 1;
         this._nsObj = new Object();
         this._ns.client = this._nsObj;
         this._video = new Video();
         this.addChild(this._video);
         this._video.smoothing = true;
         this._video.attachNetStream(this._ns);
         this._video.visible = false;
         this.createADButton();
      }
      
      protected function addEvents() : void {
         this._nsObj.onMetaData = function(param1:Object):void
         {
            onVideoMetaData(param1);
         };
         this._nsObj.onLastSecond = function(param1:Object):void
         {
         };
         this._nsObj.onLastKeyframe = function(param1:Object):void
         {
         };
         this._nsObj.onPlayStatus = function(param1:Object):void
         {
         };
         this._ns.addEventListener(NetStatusEvent.NET_STATUS,this.onNsNetStatus);
         this._ns.addEventListener(IOErrorEvent.IO_ERROR,this.onNSIOError);
         this._ns.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onNSSecurityError);
         this.addEventListener(Event.ENTER_FRAME,this.onThisEnterFrame);
      }
      
      protected function onNSSecurityError(param1:Event) : void {
         this.onNSError(ADPlayerEvent.PLAY_IO_ERROR);
      }
      
      protected function onNSIOError(param1:IOErrorEvent) : void {
         this.onNSError(ADPlayerEvent.PLAY_SECURITY_ERROR);
      }
      
      protected function onNSError(param1:String) : void {
         this.removeTimeOut();
         this._isError = true;
         if(!this._loadCompleted)
         {
            this._loadCompleted = true;
            this.dispatchEvent(new ADPlayerEvent(ADPlayerEvent.LOAD_COMPLETE));
         }
         var _loc2_:ADPlayerEvent = new ADPlayerEvent(param1);
         _loc2_.id = this._id;
         this.dispatchEvent(_loc2_);
      }
      
      protected function onVideoMetaData(param1:Object) : void {
         this._vw = !param1.width?320:param1.width;
         this._vh = !param1.height?240:param1.height;
         this._duration = param1.duration;
         var _loc2_:ADPlayerEvent = new ADPlayerEvent(ADPlayerEvent.META_DATA);
         _loc2_.totalTime = int(this._duration);
         this.dispatchEvent(_loc2_);
         this.adjustWH(this._width,this._height);
         if(this._volume != -1)
         {
            this.setVolume(this._volume);
         }
      }
      
      protected function onNsNetStatus(param1:NetStatusEvent) : void {
         var _loc2_:ADPlayerEvent = null;
         switch(param1.info.code)
         {
            case "NetStream.Play.Stop":
               if(!this._playOver)
               {
                  this._playOver = true;
                  _loc2_ = new ADPlayerEvent(ADPlayerEvent.PLAY_OVER);
               }
               break;
            case "NetStream.Buffer.Full":
               if(!this._firstFull)
               {
                  this._ns.bufferTime = 10;
                  this._firstFull = true;
               }
               if(!this._started)
               {
                  this._started = true;
                  if(!this._preLoad)
                  {
                     this._ns.resume();
                     _loc2_ = new ADPlayerEvent(ADPlayerEvent.PLAY_START);
                     this._video.visible = true;
                     this.removeTimeOut();
                  }
               }
               break;
            case "NetStream.Buffer.Empty":
               this._ns.bufferTime = 1;
               this._firstFull = false;
               break;
            case "NetStream.Play.StreamNotFound":
            case "NetStream.Play.Failed":
               this.onNSError(ADPlayerEvent.PLAY_STREAM_ERROR);
               break;
            case "NetStream.Seek.InvalidTime":
               this._ns.seek(param1.info.details);
               break;
         }
         if(_loc2_)
         {
            this.dispatchEvent(_loc2_);
         }
      }
      
      protected function onThisEnterFrame(param1:Event) : void {
         var _loc2_:* = 0;
         var _loc3_:Object = null;
         var _loc4_:* = NaN;
         var _loc5_:ADPlayerEvent = null;
         if((this._SU) && this._SU.length > 0)
         {
            _loc2_ = 0;
            while(_loc2_ < this._SU.length)
            {
               _loc3_ = this._SU[_loc2_] as Object;
               _loc4_ = _loc3_.T;
               if(this._ns.time >= _loc4_)
               {
                  this.dispatchEvent(new ADPlayerEvent(ADPlayerEvent.ARRIVE_POINT,_loc3_.U));
                  this._SU.splice(_loc2_,1);
               }
               _loc2_++;
            }
         }
         if(!this._loadCompleted && (this._ns.bytesTotal))
         {
            if(this._ns.bytesLoaded == this._ns.bytesTotal)
            {
               this._loadCompleted = true;
               this.dispatchEvent(new ADPlayerEvent(ADPlayerEvent.LOAD_COMPLETE));
            }
         }
         if((!this._playOver && this._ns.time) && (this._duration) && this._ns.time + 0.1 >= this._duration)
         {
            this._playOver = true;
            _loc5_ = new ADPlayerEvent(ADPlayerEvent.PLAY_OVER);
            this.dispatchEvent(_loc5_);
         }
      }
      
      protected function adjustWH(param1:Number, param2:Number) : void {
         var _loc3_:Number = 0;
         var _loc4_:Number = 0;
         if(this._vw / this._vh > param1 / param2)
         {
            _loc3_ = param1;
            _loc4_ = param1 * this._vh / this._vw;
         }
         else
         {
            _loc4_ = param2;
            _loc3_ = param2 * this._vw / this._vh;
         }
         this._video.width = _loc3_;
         this._video.height = _loc4_;
         this._video.x = (this._width - _loc3_) / 2;
         this._video.y = (this._height - _loc4_) / 2;
         this._layer.width = param1;
         this._layer.height = param2;
         this._layer.x = 0;
         this._layer.y = 0;
      }
      
      public function preload() : void {
         this._preLoad = true;
         this._ns.play(this._adData.RS);
         this._ns.pause();
      }
      
      public function play(param1:String = "") : void {
         if(this._preLoad)
         {
            this._preLoad = false;
            this._ns.resume();
            if(this._started)
            {
               this.dispatchEvent(new ADPlayerEvent(ADPlayerEvent.PLAY_START));
               this._video.visible = true;
               this.removeTimeOut();
            }
            else
            {
               this.removeTimeOut();
               this._timeoutId = setTimeout(this.canclePlay,15000);
            }
         }
         else
         {
            this._ns.play(this._adData.RS);
            this.removeTimeOut();
            this._timeoutId = setTimeout(this.canclePlay,15000);
         }
      }
      
      protected function canclePlay() : void {
         this._ns.close();
         this.dispatchEvent(new ADPlayerEvent(ADPlayerEvent.TIME_OUT));
      }
      
      protected function removeTimeOut() : void {
         if(this._timeoutId)
         {
            clearTimeout(this._timeoutId);
         }
      }
      
      public function get totalTime() : Number {
         var _loc1_:Number = 0;
         if(this._duration)
         {
            _loc1_ = this._duration;
         }
         return _loc1_;
      }
      
      public function get nowTime() : Number {
         if(!this._ns.time)
         {
            return 0;
         }
         return this._ns.time;
      }
      
      public function get playStarted() : Boolean {
         return this._started;
      }
      
      public function setWH(param1:Number, param2:Number) : void {
         this._width = param1;
         this._height = param2;
         this.adjustWH(this._width,this._height);
      }
      
      override public function set width(param1:Number) : void {
         this._width = param1;
         this.adjustWH(this._width,this._height);
      }
      
      override public function get width() : Number {
         return this._width;
      }
      
      override public function set height(param1:Number) : void {
         this._height = param1;
         this.adjustWH(this._width,this._height);
      }
      
      override public function get height() : Number {
         return this._height;
      }
      
      public function setVolume(param1:Number) : void {
         var _loc2_:SoundTransform = null;
         if(this._ns)
         {
            _loc2_ = this._ns.soundTransform;
            _loc2_.volume = param1 * 0.6;
            this._ns.soundTransform = _loc2_;
         }
         this._volume = param1;
      }
      
      public function dispose() : void {
         this._ns.close();
         this._nc.close();
         this._video.visible = false;
         if(this.contains(this._layer))
         {
            this.removeChild(this._layer);
         }
         this._layer.visible = false;
         this.removeEventListener(Event.ENTER_FRAME,this.onThisEnterFrame);
      }
      
      public function pause() : void {
         if(!this._ns)
         {
            return;
         }
         this._ns.pause();
      }
      
      public function resume() : void {
         if(!this._ns)
         {
            return;
         }
         this._ns.resume();
      }
      
      public function set id(param1:int) : void {
         this._id = param1;
      }
      
      public function get id() : int {
         return this._id;
      }
      
      public function get isError() : Boolean {
         return this._isError;
      }
      
      protected function createADButton() : void {
         this._layer = DrawUtil.RectangleSprite(0,0,100,100,16777215,0.0,null,0);
         this._layer.buttonMode = true;
         this._layer.useHandCursor = true;
         this.addChild(this._layer);
      }
   }
}
